Ext.ns('AIR2.UI');
/***************
 * AIR2 JsonDataView
 *
 * Dataview that auto-configures itself with an Ext.data.JsonStore
 *
 * @class AIR2.UI.JsonDataView
 * @extends AIR2.UI.DataView
 * @xtype air2jsdv
 * @cfg {Ext.Template} tpl
 *   Template to render in the dataview
 * @cfg {String} itemSelector
 *   A css selector to use with the tpl
 * @cfg {Object/Array} data
 *   Initial json data to load tpl store with, rather than an Ajax load
 * @cfg {String} url
 *   Restful url for the store tpl to use
 *
 */
AIR2.UI.JsonDataView = function(config) {
    // delete undefined properties
    for (var prop in config) {
        if (!Ext.isDefined(config[prop])) delete config[prop];
    }

    // make sure the url ends in ".json"
    if (config.url && config.url.search(/\.json$/) < 0) config.url += '.json';

    // create the json store
    var storeCfg = {autoSave: false, autoDestroy: true, restful: true, remoteSort: true};
    storeCfg.autoLoad = (config.url && !Ext.isDefined(config.data)) ? true :false;
    storeCfg.writer = new Ext.data.JsonWriter({encode: true, writeAllFields: false});
    Ext.apply(storeCfg, config);
    if (!storeCfg.listeners) storeCfg.listeners = {};
    storeCfg.listeners.metachange = this.updateMeta;
    if (Ext.isEmpty(storeCfg.data)) delete storeCfg.data; //ignore null data
    config.store = new Ext.data.JsonStore(storeCfg);

    // remove "data" from config (it was used by store, if it existed)
    delete config.data;

    // if tpl wasn't provided, setup a blank dataview
    if (!config.tpl) {
        config.tpl = new Ext.Template('<div class="dataview-empty"></div>');
        config.itemSelector = '.dataview-empty';
        config.style = 'display:none';
    }

    // call parent constructor
    AIR2.UI.JsonDataView.superclass.constructor.call(this, config);
}
Ext.extend(AIR2.UI.JsonDataView, AIR2.UI.DataView, {
    emptyText: '<div class="air2-panel-empty"><h3>(none)</h3></div>',
    deferEmptyText: false,
    trackOver: true,
    renderEmpty: false,
    refresh: function() {
        if (this.renderEmpty) {
            this.clearSelections(false, true);
            var el = this.getTemplateTarget();
            el.update("");
            var records = this.store.getRange();
            this.tpl.overwrite(el, this.collectData(records, 0));
            this.all.fill(Ext.query(this.itemSelector, el.dom));
            this.updateIndexes(0);
        }
        else {
            AIR2.UI.JsonDataView.superclass.refresh.call(this);
        }
    },
    updateMeta: function(store, meta) {
        if (meta.start) store.setBaseParam('start', meta.start);
        if (meta.limit) store.setBaseParam('limit', meta.limit);
        if (meta.sortInfo && meta.sortInfo.field)
            store.setBaseParam('sort', meta.sortInfo.field);
        if (meta.sortInfo && meta.sortInfo.direction)
            store.setBaseParam('dir', meta.sortInfo.direction);
    }
});
Ext.reg('air2jsdv', AIR2.UI.JsonDataView);